<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>转-commander | 仲灏小栈</title>
    <meta name="generator" content="VuePress 1.9.5">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="专注web全栈学习与总结。JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github等技术文章。">
    <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
    <meta name="baidu-site-verification" content="code-5CGlqw1B4p">
    <meta name="google-site-verification" content="PbO8RiLRwDnNwqNFNGnaxZnEFB3CqcGMCGoloYfQXV0">
    <meta name="theme-color" content="#65d1a0">
    
    <link rel="preload" href="/assets/css/0.styles.6c1f7aaa.css" as="style"><link rel="preload" href="/assets/js/app.6d26e8d4.js" as="script"><link rel="preload" href="/assets/js/2.8273876b.js" as="script"><link rel="preload" href="/assets/js/51.acd0d321.js" as="script"><link rel="prefetch" href="/assets/js/10.cb095657.js"><link rel="prefetch" href="/assets/js/100.fbcd69a7.js"><link rel="prefetch" href="/assets/js/101.8fad45f8.js"><link rel="prefetch" href="/assets/js/102.16b0b3d1.js"><link rel="prefetch" href="/assets/js/103.c2af4d4a.js"><link rel="prefetch" href="/assets/js/104.d37d4660.js"><link rel="prefetch" href="/assets/js/105.27e54103.js"><link rel="prefetch" href="/assets/js/106.e4098682.js"><link rel="prefetch" href="/assets/js/107.8efeef5d.js"><link rel="prefetch" href="/assets/js/108.6c305bbf.js"><link rel="prefetch" href="/assets/js/109.6f1c6613.js"><link rel="prefetch" href="/assets/js/11.d1f39b42.js"><link rel="prefetch" href="/assets/js/110.6f264ba6.js"><link rel="prefetch" href="/assets/js/111.1b6294ef.js"><link rel="prefetch" href="/assets/js/112.2ad16154.js"><link rel="prefetch" href="/assets/js/113.a40acefc.js"><link rel="prefetch" href="/assets/js/114.d44cb107.js"><link rel="prefetch" href="/assets/js/115.bd15b919.js"><link rel="prefetch" href="/assets/js/116.778a9058.js"><link rel="prefetch" href="/assets/js/117.19ff15b7.js"><link rel="prefetch" href="/assets/js/118.2630ca8d.js"><link rel="prefetch" href="/assets/js/119.604bd439.js"><link rel="prefetch" href="/assets/js/12.892c0d9f.js"><link rel="prefetch" href="/assets/js/120.43a964f8.js"><link rel="prefetch" href="/assets/js/121.5993f55a.js"><link rel="prefetch" href="/assets/js/122.6c054fcf.js"><link rel="prefetch" href="/assets/js/123.95ddd9a3.js"><link rel="prefetch" href="/assets/js/124.c3a9510e.js"><link rel="prefetch" href="/assets/js/125.612c461a.js"><link rel="prefetch" href="/assets/js/126.ccdb194e.js"><link rel="prefetch" href="/assets/js/127.865789a0.js"><link rel="prefetch" href="/assets/js/128.9e0bc723.js"><link rel="prefetch" href="/assets/js/129.68515ee6.js"><link rel="prefetch" href="/assets/js/13.ea744146.js"><link rel="prefetch" href="/assets/js/130.32a31759.js"><link rel="prefetch" href="/assets/js/131.467e7aaf.js"><link rel="prefetch" href="/assets/js/132.3bb05df9.js"><link rel="prefetch" href="/assets/js/133.52bbf931.js"><link rel="prefetch" href="/assets/js/134.ee00e869.js"><link rel="prefetch" href="/assets/js/135.aadd30fb.js"><link rel="prefetch" href="/assets/js/136.6b5bb775.js"><link rel="prefetch" href="/assets/js/137.9d66b052.js"><link rel="prefetch" href="/assets/js/138.1df874a9.js"><link rel="prefetch" href="/assets/js/139.bd1e48e9.js"><link rel="prefetch" href="/assets/js/14.c755eadf.js"><link rel="prefetch" href="/assets/js/140.2a849d58.js"><link rel="prefetch" href="/assets/js/141.28c22cf9.js"><link rel="prefetch" href="/assets/js/142.e99735ee.js"><link rel="prefetch" href="/assets/js/143.0666fdce.js"><link rel="prefetch" href="/assets/js/144.216c1efe.js"><link rel="prefetch" href="/assets/js/145.fad2c93a.js"><link rel="prefetch" href="/assets/js/146.01533714.js"><link rel="prefetch" href="/assets/js/147.1af38e50.js"><link rel="prefetch" href="/assets/js/148.84844c3d.js"><link rel="prefetch" href="/assets/js/149.49ce06ed.js"><link rel="prefetch" href="/assets/js/15.7a29d3f4.js"><link rel="prefetch" href="/assets/js/150.234bdfef.js"><link rel="prefetch" href="/assets/js/151.89e0796a.js"><link rel="prefetch" href="/assets/js/152.389ced87.js"><link rel="prefetch" href="/assets/js/153.07285920.js"><link rel="prefetch" href="/assets/js/154.965fc5c1.js"><link rel="prefetch" href="/assets/js/155.7bf58a9a.js"><link rel="prefetch" href="/assets/js/156.99e400e4.js"><link rel="prefetch" href="/assets/js/157.b6edd5ab.js"><link rel="prefetch" href="/assets/js/158.af656e52.js"><link rel="prefetch" href="/assets/js/159.4b3d37d2.js"><link rel="prefetch" href="/assets/js/16.5c417580.js"><link rel="prefetch" href="/assets/js/160.fa1e9d1b.js"><link rel="prefetch" href="/assets/js/161.43d6f2b4.js"><link rel="prefetch" href="/assets/js/162.c0e7e60f.js"><link rel="prefetch" href="/assets/js/163.c78d27b4.js"><link rel="prefetch" href="/assets/js/164.7ee2986b.js"><link rel="prefetch" href="/assets/js/165.bbc6516b.js"><link rel="prefetch" href="/assets/js/166.90e15253.js"><link rel="prefetch" href="/assets/js/167.18578ca6.js"><link rel="prefetch" href="/assets/js/168.c52ad494.js"><link rel="prefetch" href="/assets/js/169.c34b2d53.js"><link rel="prefetch" href="/assets/js/17.76fdc3b2.js"><link rel="prefetch" href="/assets/js/170.c97f6096.js"><link rel="prefetch" href="/assets/js/171.358f0f02.js"><link rel="prefetch" href="/assets/js/172.30f03133.js"><link rel="prefetch" href="/assets/js/173.b72d5243.js"><link rel="prefetch" href="/assets/js/174.4e333643.js"><link rel="prefetch" href="/assets/js/175.2c07b836.js"><link rel="prefetch" href="/assets/js/176.29bcb24c.js"><link rel="prefetch" href="/assets/js/177.102b3f0f.js"><link rel="prefetch" href="/assets/js/178.5862bde0.js"><link rel="prefetch" href="/assets/js/179.0da54072.js"><link rel="prefetch" href="/assets/js/18.f857e697.js"><link rel="prefetch" href="/assets/js/180.393e8099.js"><link rel="prefetch" href="/assets/js/181.418f9fe8.js"><link rel="prefetch" href="/assets/js/182.e65424fa.js"><link rel="prefetch" href="/assets/js/183.3b3cb092.js"><link rel="prefetch" href="/assets/js/184.73f7a76a.js"><link rel="prefetch" href="/assets/js/185.537623fb.js"><link rel="prefetch" href="/assets/js/186.df802468.js"><link rel="prefetch" href="/assets/js/187.1095bafd.js"><link rel="prefetch" href="/assets/js/188.1d2acd1a.js"><link rel="prefetch" href="/assets/js/189.dc6a5186.js"><link rel="prefetch" href="/assets/js/19.8c1d0f81.js"><link rel="prefetch" href="/assets/js/190.a279b4dd.js"><link rel="prefetch" href="/assets/js/191.3abaa15f.js"><link rel="prefetch" href="/assets/js/192.4d0dd04e.js"><link rel="prefetch" href="/assets/js/193.2c698e6f.js"><link rel="prefetch" href="/assets/js/194.4243beaa.js"><link rel="prefetch" href="/assets/js/195.7c13b124.js"><link rel="prefetch" href="/assets/js/196.b18118bf.js"><link rel="prefetch" href="/assets/js/197.2abfac38.js"><link rel="prefetch" href="/assets/js/198.407f0a8b.js"><link rel="prefetch" href="/assets/js/199.d4e97578.js"><link rel="prefetch" href="/assets/js/20.47831bf7.js"><link rel="prefetch" href="/assets/js/200.86ce407c.js"><link rel="prefetch" href="/assets/js/201.56a65a3f.js"><link rel="prefetch" href="/assets/js/202.f852c6e8.js"><link rel="prefetch" href="/assets/js/203.92530d7a.js"><link rel="prefetch" href="/assets/js/204.ce612269.js"><link rel="prefetch" href="/assets/js/205.b14b2d70.js"><link rel="prefetch" href="/assets/js/206.b5112828.js"><link rel="prefetch" href="/assets/js/207.a4becd8a.js"><link rel="prefetch" href="/assets/js/208.f82e2766.js"><link rel="prefetch" href="/assets/js/209.11566ec8.js"><link rel="prefetch" href="/assets/js/21.8580b0b4.js"><link rel="prefetch" href="/assets/js/210.bf848e0e.js"><link rel="prefetch" href="/assets/js/211.6db52324.js"><link rel="prefetch" href="/assets/js/212.c9a03071.js"><link rel="prefetch" href="/assets/js/213.734c775f.js"><link rel="prefetch" href="/assets/js/214.7654a416.js"><link rel="prefetch" href="/assets/js/215.34ad7e3d.js"><link rel="prefetch" href="/assets/js/216.aabcd184.js"><link rel="prefetch" href="/assets/js/217.26ffe7d6.js"><link rel="prefetch" href="/assets/js/218.d8ceaf2e.js"><link rel="prefetch" href="/assets/js/219.82bad7ef.js"><link rel="prefetch" href="/assets/js/22.2d87ea68.js"><link rel="prefetch" href="/assets/js/220.0c35dcdf.js"><link rel="prefetch" href="/assets/js/221.8657d7c1.js"><link rel="prefetch" href="/assets/js/222.291af8a7.js"><link rel="prefetch" href="/assets/js/223.e8ce5ce0.js"><link rel="prefetch" href="/assets/js/224.2c3c1268.js"><link rel="prefetch" href="/assets/js/225.b31429da.js"><link rel="prefetch" href="/assets/js/226.8ec29e66.js"><link rel="prefetch" href="/assets/js/227.84469b41.js"><link rel="prefetch" href="/assets/js/228.cd6b9967.js"><link rel="prefetch" href="/assets/js/229.e5497c23.js"><link rel="prefetch" href="/assets/js/23.c1a929a6.js"><link rel="prefetch" href="/assets/js/230.643fdbb6.js"><link rel="prefetch" href="/assets/js/231.22457248.js"><link rel="prefetch" href="/assets/js/232.7e94e6f0.js"><link rel="prefetch" href="/assets/js/233.1083085e.js"><link rel="prefetch" href="/assets/js/234.6ce5972b.js"><link rel="prefetch" href="/assets/js/235.bc1d1e7e.js"><link rel="prefetch" href="/assets/js/236.f510d892.js"><link rel="prefetch" href="/assets/js/237.4690c583.js"><link rel="prefetch" href="/assets/js/238.ef69b085.js"><link rel="prefetch" href="/assets/js/239.e7cbaef4.js"><link rel="prefetch" href="/assets/js/24.707a9b9d.js"><link rel="prefetch" href="/assets/js/240.02e1ce4d.js"><link rel="prefetch" href="/assets/js/241.8ccdecd0.js"><link rel="prefetch" href="/assets/js/242.25a5eeac.js"><link rel="prefetch" href="/assets/js/243.80dc6cf3.js"><link rel="prefetch" href="/assets/js/244.d2d264c1.js"><link rel="prefetch" href="/assets/js/245.8646c059.js"><link rel="prefetch" href="/assets/js/246.0d788a8c.js"><link rel="prefetch" href="/assets/js/247.211b425b.js"><link rel="prefetch" href="/assets/js/248.15e5b430.js"><link rel="prefetch" href="/assets/js/249.b28c41d2.js"><link rel="prefetch" href="/assets/js/25.e47a8360.js"><link rel="prefetch" href="/assets/js/250.e4621bbf.js"><link rel="prefetch" href="/assets/js/251.2f49ea1d.js"><link rel="prefetch" href="/assets/js/252.c567824c.js"><link rel="prefetch" href="/assets/js/253.7eb2a6c2.js"><link rel="prefetch" href="/assets/js/254.adf8b2ad.js"><link rel="prefetch" href="/assets/js/255.43d07402.js"><link rel="prefetch" href="/assets/js/256.d49b01ff.js"><link rel="prefetch" href="/assets/js/257.cc61cd1a.js"><link rel="prefetch" href="/assets/js/258.e46ee926.js"><link rel="prefetch" href="/assets/js/259.5c84c742.js"><link rel="prefetch" href="/assets/js/26.db56ba8d.js"><link rel="prefetch" href="/assets/js/260.05cc8cff.js"><link rel="prefetch" href="/assets/js/261.1891f9c6.js"><link rel="prefetch" href="/assets/js/262.0913812a.js"><link rel="prefetch" href="/assets/js/263.a6836819.js"><link rel="prefetch" href="/assets/js/264.363da610.js"><link rel="prefetch" href="/assets/js/265.2cbfbe9f.js"><link rel="prefetch" href="/assets/js/266.555ca792.js"><link rel="prefetch" href="/assets/js/267.83f4c202.js"><link rel="prefetch" href="/assets/js/268.6dea35af.js"><link rel="prefetch" href="/assets/js/269.cbc5306b.js"><link rel="prefetch" href="/assets/js/27.0b0c1f7f.js"><link rel="prefetch" href="/assets/js/270.6bc430c5.js"><link rel="prefetch" href="/assets/js/271.f5105765.js"><link rel="prefetch" href="/assets/js/272.e1541ef7.js"><link rel="prefetch" href="/assets/js/273.158c7593.js"><link rel="prefetch" href="/assets/js/274.efb98c5b.js"><link rel="prefetch" href="/assets/js/275.cf2c3b21.js"><link rel="prefetch" href="/assets/js/276.0bf20a2d.js"><link rel="prefetch" href="/assets/js/277.2e4ddf6c.js"><link rel="prefetch" href="/assets/js/278.7f8b14ac.js"><link rel="prefetch" href="/assets/js/279.83a65d2f.js"><link rel="prefetch" href="/assets/js/28.a5d8ae18.js"><link rel="prefetch" href="/assets/js/280.47de1643.js"><link rel="prefetch" href="/assets/js/281.1d5cab51.js"><link rel="prefetch" href="/assets/js/282.b2299bce.js"><link rel="prefetch" href="/assets/js/283.fdad201b.js"><link rel="prefetch" href="/assets/js/284.cdf83f8f.js"><link rel="prefetch" href="/assets/js/285.caee6eb0.js"><link rel="prefetch" href="/assets/js/286.6b211023.js"><link rel="prefetch" href="/assets/js/287.e9e93db3.js"><link rel="prefetch" href="/assets/js/288.5892d2e1.js"><link rel="prefetch" href="/assets/js/289.48074c49.js"><link rel="prefetch" href="/assets/js/29.0bc8ea25.js"><link rel="prefetch" href="/assets/js/290.b478b2ab.js"><link rel="prefetch" href="/assets/js/291.18239d2b.js"><link rel="prefetch" href="/assets/js/292.9f6b48b4.js"><link rel="prefetch" href="/assets/js/293.70a0b924.js"><link rel="prefetch" href="/assets/js/294.bc5f021a.js"><link rel="prefetch" href="/assets/js/295.dfa72324.js"><link rel="prefetch" href="/assets/js/296.fb717477.js"><link rel="prefetch" href="/assets/js/297.2b2daf25.js"><link rel="prefetch" href="/assets/js/298.0ed3ce25.js"><link rel="prefetch" href="/assets/js/299.c59a3f82.js"><link rel="prefetch" href="/assets/js/3.d68e1d13.js"><link rel="prefetch" href="/assets/js/30.063f8dbe.js"><link rel="prefetch" href="/assets/js/300.f423c130.js"><link rel="prefetch" href="/assets/js/301.a11bcaa5.js"><link rel="prefetch" href="/assets/js/302.5a45b99e.js"><link rel="prefetch" href="/assets/js/303.80ac16da.js"><link rel="prefetch" href="/assets/js/304.f773afc2.js"><link rel="prefetch" href="/assets/js/305.52f1b3ba.js"><link rel="prefetch" href="/assets/js/306.966f2eef.js"><link rel="prefetch" href="/assets/js/307.c22143cf.js"><link rel="prefetch" href="/assets/js/308.a6dfec6e.js"><link rel="prefetch" href="/assets/js/309.377e4f0b.js"><link rel="prefetch" href="/assets/js/31.69d8f2c2.js"><link rel="prefetch" href="/assets/js/310.a76309fc.js"><link rel="prefetch" href="/assets/js/311.0706d5ca.js"><link rel="prefetch" href="/assets/js/312.40513d91.js"><link rel="prefetch" href="/assets/js/313.e36621af.js"><link rel="prefetch" href="/assets/js/314.3d4325be.js"><link rel="prefetch" href="/assets/js/315.72480163.js"><link rel="prefetch" href="/assets/js/316.83f31163.js"><link rel="prefetch" href="/assets/js/317.872fd713.js"><link rel="prefetch" href="/assets/js/318.0dc2aea4.js"><link rel="prefetch" href="/assets/js/319.454e4745.js"><link rel="prefetch" href="/assets/js/32.50aa541b.js"><link rel="prefetch" href="/assets/js/320.d9116236.js"><link rel="prefetch" href="/assets/js/321.b06a7a4e.js"><link rel="prefetch" href="/assets/js/322.41bcaaf1.js"><link rel="prefetch" href="/assets/js/323.f01ba88b.js"><link rel="prefetch" href="/assets/js/324.9a736f30.js"><link rel="prefetch" href="/assets/js/325.49b1193e.js"><link rel="prefetch" href="/assets/js/326.5a07b4a0.js"><link rel="prefetch" href="/assets/js/327.aceece56.js"><link rel="prefetch" href="/assets/js/328.e03489c3.js"><link rel="prefetch" href="/assets/js/329.5673fdef.js"><link rel="prefetch" href="/assets/js/33.4aa00ff3.js"><link rel="prefetch" href="/assets/js/330.246e74fa.js"><link rel="prefetch" href="/assets/js/331.e1cffa6b.js"><link rel="prefetch" href="/assets/js/332.2856b83e.js"><link rel="prefetch" href="/assets/js/333.d4c9c15a.js"><link rel="prefetch" href="/assets/js/334.13b03971.js"><link rel="prefetch" href="/assets/js/335.0b8ed702.js"><link rel="prefetch" href="/assets/js/336.3e71f28d.js"><link rel="prefetch" href="/assets/js/337.f929f08c.js"><link rel="prefetch" href="/assets/js/338.01a6cfbe.js"><link rel="prefetch" href="/assets/js/339.5cd647ad.js"><link rel="prefetch" href="/assets/js/34.b25c4a87.js"><link rel="prefetch" href="/assets/js/340.f941cae0.js"><link rel="prefetch" href="/assets/js/341.aa01a6b1.js"><link rel="prefetch" href="/assets/js/342.edd59a58.js"><link rel="prefetch" href="/assets/js/343.237c716b.js"><link rel="prefetch" href="/assets/js/344.06e59f2b.js"><link rel="prefetch" href="/assets/js/345.ddd4f6fe.js"><link rel="prefetch" href="/assets/js/346.70fe3c42.js"><link rel="prefetch" href="/assets/js/347.d39ad15e.js"><link rel="prefetch" href="/assets/js/348.1b135410.js"><link rel="prefetch" href="/assets/js/349.fd78d783.js"><link rel="prefetch" href="/assets/js/35.a1af8744.js"><link rel="prefetch" href="/assets/js/350.b10e7d69.js"><link rel="prefetch" href="/assets/js/351.a6ae4318.js"><link rel="prefetch" href="/assets/js/352.3117739a.js"><link rel="prefetch" href="/assets/js/353.99e022f1.js"><link rel="prefetch" href="/assets/js/354.a444c2d8.js"><link rel="prefetch" href="/assets/js/355.13d044aa.js"><link rel="prefetch" href="/assets/js/356.d2cfe5d0.js"><link rel="prefetch" href="/assets/js/357.eb44f05f.js"><link rel="prefetch" href="/assets/js/358.394c15db.js"><link rel="prefetch" href="/assets/js/359.6c541551.js"><link rel="prefetch" href="/assets/js/36.5558b717.js"><link rel="prefetch" href="/assets/js/360.65af88d5.js"><link rel="prefetch" href="/assets/js/361.d9b3c889.js"><link rel="prefetch" href="/assets/js/362.37ec0a7d.js"><link rel="prefetch" href="/assets/js/363.49690222.js"><link rel="prefetch" href="/assets/js/364.419b3dab.js"><link rel="prefetch" href="/assets/js/365.1f79df07.js"><link rel="prefetch" href="/assets/js/366.fa710c6f.js"><link rel="prefetch" href="/assets/js/367.497eeda5.js"><link rel="prefetch" href="/assets/js/368.45cf08f6.js"><link rel="prefetch" href="/assets/js/369.80a60b6a.js"><link rel="prefetch" href="/assets/js/37.511798d2.js"><link rel="prefetch" href="/assets/js/370.61b161b4.js"><link rel="prefetch" href="/assets/js/371.545f0249.js"><link rel="prefetch" href="/assets/js/372.d383f896.js"><link rel="prefetch" href="/assets/js/373.df22361b.js"><link rel="prefetch" href="/assets/js/374.0b9872ee.js"><link rel="prefetch" href="/assets/js/375.76bbca97.js"><link rel="prefetch" href="/assets/js/376.1f6f8210.js"><link rel="prefetch" href="/assets/js/377.3dd0dc75.js"><link rel="prefetch" href="/assets/js/378.32cac01d.js"><link rel="prefetch" href="/assets/js/379.6682a524.js"><link rel="prefetch" href="/assets/js/38.2dfd8d32.js"><link rel="prefetch" href="/assets/js/380.1ab9ec1d.js"><link rel="prefetch" href="/assets/js/381.cea4ec7d.js"><link rel="prefetch" href="/assets/js/382.fc51cce1.js"><link rel="prefetch" href="/assets/js/383.489718b4.js"><link rel="prefetch" href="/assets/js/384.a7a1c6d7.js"><link rel="prefetch" href="/assets/js/385.7b2d3a89.js"><link rel="prefetch" href="/assets/js/386.f0a18324.js"><link rel="prefetch" href="/assets/js/387.53063e56.js"><link rel="prefetch" href="/assets/js/388.1115759c.js"><link rel="prefetch" href="/assets/js/389.58bf8aaf.js"><link rel="prefetch" href="/assets/js/39.e8482eab.js"><link rel="prefetch" href="/assets/js/4.de17fbe4.js"><link rel="prefetch" href="/assets/js/40.c6dc3476.js"><link rel="prefetch" href="/assets/js/41.78bfe256.js"><link rel="prefetch" href="/assets/js/42.248cddd4.js"><link rel="prefetch" href="/assets/js/43.6429d3dc.js"><link rel="prefetch" href="/assets/js/44.71e9af05.js"><link rel="prefetch" href="/assets/js/45.f80fb4fc.js"><link rel="prefetch" href="/assets/js/46.3d5e7e58.js"><link rel="prefetch" href="/assets/js/47.1bbf5e9a.js"><link rel="prefetch" href="/assets/js/48.6909050b.js"><link rel="prefetch" href="/assets/js/49.9c775f82.js"><link rel="prefetch" href="/assets/js/5.59910585.js"><link rel="prefetch" href="/assets/js/50.aceb6009.js"><link rel="prefetch" href="/assets/js/52.a4f9f157.js"><link rel="prefetch" href="/assets/js/53.0f1ec943.js"><link rel="prefetch" href="/assets/js/54.6c5c2837.js"><link rel="prefetch" href="/assets/js/55.f412714f.js"><link rel="prefetch" href="/assets/js/56.64a3862c.js"><link rel="prefetch" href="/assets/js/57.d070eb7d.js"><link rel="prefetch" href="/assets/js/58.a80067c8.js"><link rel="prefetch" href="/assets/js/59.084f5e76.js"><link rel="prefetch" href="/assets/js/6.0ab71a58.js"><link rel="prefetch" href="/assets/js/60.42e0fbb5.js"><link rel="prefetch" href="/assets/js/61.b5b87c23.js"><link rel="prefetch" href="/assets/js/62.014cdbe2.js"><link rel="prefetch" href="/assets/js/63.ff1278d1.js"><link rel="prefetch" href="/assets/js/64.44a86cb2.js"><link rel="prefetch" href="/assets/js/65.1a69bd56.js"><link rel="prefetch" href="/assets/js/66.b8d9a1e9.js"><link rel="prefetch" href="/assets/js/67.48e3c0e6.js"><link rel="prefetch" href="/assets/js/68.127da596.js"><link rel="prefetch" href="/assets/js/69.77e4e1cb.js"><link rel="prefetch" href="/assets/js/7.769e6e3f.js"><link rel="prefetch" href="/assets/js/70.7863bd30.js"><link rel="prefetch" href="/assets/js/71.c5c89c65.js"><link rel="prefetch" href="/assets/js/72.030ac8a4.js"><link rel="prefetch" href="/assets/js/73.e96cf593.js"><link rel="prefetch" href="/assets/js/74.f9319fe7.js"><link rel="prefetch" href="/assets/js/75.049ce9e1.js"><link rel="prefetch" href="/assets/js/76.c71e5605.js"><link rel="prefetch" href="/assets/js/77.1e682453.js"><link rel="prefetch" href="/assets/js/78.0cce7473.js"><link rel="prefetch" href="/assets/js/79.3d32f303.js"><link rel="prefetch" href="/assets/js/8.812618a5.js"><link rel="prefetch" href="/assets/js/80.c956dc30.js"><link rel="prefetch" href="/assets/js/81.8276eaa8.js"><link rel="prefetch" href="/assets/js/82.10f6fdc6.js"><link rel="prefetch" href="/assets/js/83.cac65ec1.js"><link rel="prefetch" href="/assets/js/84.a4ce0ebf.js"><link rel="prefetch" href="/assets/js/85.d9efee67.js"><link rel="prefetch" href="/assets/js/86.cf64260c.js"><link rel="prefetch" href="/assets/js/87.e261e385.js"><link rel="prefetch" href="/assets/js/88.63b9894b.js"><link rel="prefetch" href="/assets/js/89.0ad95e30.js"><link rel="prefetch" href="/assets/js/9.c942e82a.js"><link rel="prefetch" href="/assets/js/90.77f9f729.js"><link rel="prefetch" href="/assets/js/91.3fcbf2d5.js"><link rel="prefetch" href="/assets/js/92.8b57334e.js"><link rel="prefetch" href="/assets/js/93.6095e56a.js"><link rel="prefetch" href="/assets/js/94.c616143f.js"><link rel="prefetch" href="/assets/js/95.a9b61cf9.js"><link rel="prefetch" href="/assets/js/96.a3896e7e.js"><link rel="prefetch" href="/assets/js/97.0318332c.js"><link rel="prefetch" href="/assets/js/98.17b8173b.js"><link rel="prefetch" href="/assets/js/99.d299c156.js">
    <link rel="stylesheet" href="/assets/css/0.styles.6c1f7aaa.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu have-body-img"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="仲灏小栈" class="logo"> <span class="site-name can-hide">仲灏小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/web/" class="nav-link">大前端</a></div><div class="nav-item"><a href="/backend-opt/" class="nav-link">后端&amp;运维</a></div><div class="nav-item"><a href="/other-tech/" class="nav-link">其他技术</a></div><div class="nav-item"><a href="/tech-live/" class="nav-link">生活</a></div><div class="nav-item"><a href="/about/" class="nav-link">关于我</a></div><div class="nav-item"><a href="/favorite/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/izhaong" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/izhaong/izhaong.com-oss/site-data/avatar/avatar-wx.jpg"> <div class="blogger-info"><h3>仲灏</h3> <span>诚意, 正心, 格物, 致知</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/web/" class="nav-link">大前端</a></div><div class="nav-item"><a href="/backend-opt/" class="nav-link">后端&amp;运维</a></div><div class="nav-item"><a href="/other-tech/" class="nav-link">其他技术</a></div><div class="nav-item"><a href="/tech-live/" class="nav-link">生活</a></div><div class="nav-item"><a href="/about/" class="nav-link">关于我</a></div><div class="nav-item"><a href="/favorite/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/izhaong" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/note/infrastructure/" class="sidebar-link">《前端项目基础建设》</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>HTML&amp;CSS</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>JavaScript&amp;TypeScript</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Node</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/febcd5/" class="sidebar-link">nvm 使用</a></li><li><a href="/pages/b378af/" class="sidebar-link">nvm 深度使用解析</a></li><li><a href="/pages/96c6f8/" class="sidebar-link">pnpm 学习</a></li><li><a href="/pages/8743eb/" class="sidebar-link">pnpm 使用及问题处理</a></li><li><a href="/pages/d15ce2/" class="sidebar-link">npm yarn pnpm命令对比</a></li><li><a href="/pages/037850/" class="sidebar-link">koa学习</a></li><li><a href="/pages/3e0a1c/" class="sidebar-link">restful api笔记</a></li><li><a href="/pages/1210/" class="sidebar-link">Egg.js 技巧</a></li><li><a href="/pages/9da7c4/" class="sidebar-link">Sequelize 学习笔记</a></li><li><a href="/pages/110928/" class="sidebar-link">npm 使用记录</a></li><li><a href="/pages/50e782/" class="sidebar-link">node 记错修复</a></li><li><a href="/pages/34d0c9/" class="sidebar-link">npm 仓库</a></li><li><a href="/pages/30de9a/" class="sidebar-link">安装npm的过程中发生了什么</a></li><li><a href="/pages/5565db/" class="sidebar-link">想用而找不到的 npm 插件</a></li><li><a href="/pages/f3d9dd/" class="sidebar-link">node全局脚本原理解析</a></li><li><a href="/pages/e7db5c/" aria-current="page" class="active sidebar-link">转-commander</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/e7db5c/#安装" class="sidebar-link">安装</a></li><li class="sidebar-sub-header level2"><a href="/pages/e7db5c/#快速开始" class="sidebar-link">快速开始</a></li><li class="sidebar-sub-header level2"><a href="/pages/e7db5c/#声明-program-变量" class="sidebar-link">声明 program 变量</a></li><li class="sidebar-sub-header level2"><a href="/pages/e7db5c/#选项" class="sidebar-link">选项</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#常用选项类型-boolean-型选项和带参数选项" class="sidebar-link">常用选项类型，boolean 型选项和带参数选项</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#选项的默认值" class="sidebar-link">选项的默认值</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#其他的选项类型-取反选项-以及可选参数的选项" class="sidebar-link">其他的选项类型，取反选项，以及可选参数的选项</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#必填选项" class="sidebar-link">必填选项</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#变长参数选项" class="sidebar-link">变长参数选项</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#版本选项" class="sidebar-link">版本选项</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#其他选项配置" class="sidebar-link">其他选项配置</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#自定义选项处理" class="sidebar-link">自定义选项处理</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/e7db5c/#命令" class="sidebar-link">命令</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#命令参数" class="sidebar-link">命令参数</a></li><li class="sidebar-sub-header level4"><a href="/pages/e7db5c/#其他参数配置" class="sidebar-link">其他参数配置</a></li><li class="sidebar-sub-header level4"><a href="/pages/e7db5c/#自定义参数处理" class="sidebar-link">自定义参数处理</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#处理函数" class="sidebar-link">处理函数</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#独立的可执行-子-命令" class="sidebar-link">独立的可执行（子）命令</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#生命周期钩子" class="sidebar-link">生命周期钩子</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/e7db5c/#自动化帮助信息" class="sidebar-link">自动化帮助信息</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#自定义帮助" class="sidebar-link">自定义帮助</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#在出错后展示帮助信息" class="sidebar-link">在出错后展示帮助信息</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#使用代码展示帮助信息" class="sidebar-link">使用代码展示帮助信息</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#name" class="sidebar-link">.name</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#usage" class="sidebar-link">.usage</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#description-和-summary" class="sidebar-link">.description 和 .summary</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#helpoption-flags-description" class="sidebar-link">.helpOption(flags, description)</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#addhelpcommand" class="sidebar-link">.addHelpCommand()</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#其他帮助配置" class="sidebar-link">其他帮助配置</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/e7db5c/#自定义事件监听" class="sidebar-link">自定义事件监听</a></li><li class="sidebar-sub-header level2"><a href="/pages/e7db5c/#零碎知识" class="sidebar-link">零碎知识</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#parse-和-parseasync" class="sidebar-link">.parse() 和 .parseAsync()</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#解析配置" class="sidebar-link">解析配置</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#作为属性的遗留选项" class="sidebar-link">作为属性的遗留选项</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#typescript" class="sidebar-link">TypeScript</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#createcommand" class="sidebar-link">createCommand()</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#node-选项-如-harmony" class="sidebar-link">Node 选项，如 --harmony</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#调试子命令" class="sidebar-link">调试子命令</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#显示错误" class="sidebar-link">显示错误</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#重写退出和输出" class="sidebar-link">重写退出和输出</a></li><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#其他文档" class="sidebar-link">其他文档</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/e7db5c/#支持" class="sidebar-link">支持</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/e7db5c/#企业使用-commander" class="sidebar-link">企业使用 Commander</a></li></ul></li></ul></li><li><a href="/pages/34a68b/" class="sidebar-link">制作自己的npm包</a></li><li><a href="/pages/147a7a/" class="sidebar-link">lerna</a></li><li><a href="/pages/8c0e85/" class="sidebar-link">lerna - Lerna an Nx</a></li><li><a href="/pages/6bc0b9/" class="sidebar-link">lerna - 功能点</a></li><li><a href="/pages/f377d0/" class="sidebar-link">lerna API参考</a></li><li><a href="/pages/3de397/" class="sidebar-link">Egg.js 技巧 问题集合</a></li><li><a href="/pages/efacd5/" class="sidebar-link">Node+vue打造全栈资源分享网站</a></li><li><a href="/pages/a97403/" class="sidebar-link">脚手架核心流程开发</a></li><li><a href="/pages/e20e84/" class="sidebar-link">Untitled</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>构建</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Vue</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>React</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>小程序</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>跨端</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Electron</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>WebGL&amp;GIS</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>浏览器</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面经</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>其他</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/web/#大前端" data-v-06225672>大前端</a></li><li data-v-06225672><a href="/web/#Node" data-v-06225672>Node</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/izhaong" target="_blank" title="作者" class="beLink" data-v-06225672>仲灏</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2023-02-11</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">转-commander<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="commander-js"><a href="#commander-js" class="header-anchor">#</a> Commander.js</h1> <p><a href="https://github.com/tj/commander.js/actions?query=workflow%3A%22build%22" target="_blank" rel="noopener noreferrer"><img src="https://github.com/tj/commander.js/workflows/build/badge.svg" alt="Build Status"><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <a href="https://www.npmjs.org/package/commander" target="_blank" rel="noopener noreferrer"><img src="http://img.shields.io/npm/v/commander.svg?style=flat" alt="NPM Version"><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <a href="https://npmcharts.com/compare/commander?minimal=true" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/npm/dm/commander.svg?style=flat" alt="NPM Downloads"><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <a href="https://packagephobia.now.sh/result?p=commander" target="_blank" rel="noopener noreferrer"><img src="https://packagephobia.now.sh/badge?p=commander" alt="Install Size"><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>完整的 <a href="http://nodejs.org" target="_blank" rel="noopener noreferrer">node.js<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 命令行解决方案。</p> <p>使用其他语言阅读：<a href="/10.大前端/1030.Node/">English</a> | 简体中文</p> <ul><li><a href="#commanderjs">Commander.js</a> <ul><li><a href="#%e5%ae%89%e8%a3%85">安装</a></li> <li><a href="#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B">快速开始</a></li> <li><a href="#%e5%a3%b0%e6%98%8e-program-%e5%8f%98%e9%87%8f">声明 program 变量</a></li> <li><a href="#%e9%80%89%e9%a1%b9">选项</a> <ul><li><a href="#%e5%b8%b8%e7%94%a8%e9%80%89%e9%a1%b9%e7%b1%bb%e5%9e%8bboolean-%e5%9e%8b%e9%80%89%e9%a1%b9%e5%92%8c%e5%b8%a6%e5%8f%82%e6%95%b0%e9%80%89%e9%a1%b9">常用选项类型，boolean 型选项和带参数的选项</a></li> <li><a href="#%E9%80%89%E9%A1%B9%E7%9A%84%E9%BB%98%E8%AE%A4%E5%80%BC">选项的默认值</a></li> <li><a href="#%E5%85%B6%E4%BB%96%E7%9A%84%E9%80%89%E9%A1%B9%E7%B1%BB%E5%9E%8B%EF%BC%8C%E5%8F%96%E5%8F%8D%E9%80%89%E9%A1%B9%EF%BC%8C%E4%BB%A5%E5%8F%8A%E5%8F%AF%E9%80%89%E5%8F%82%E6%95%B0%E7%9A%84%E9%80%89%E9%A1%B9">其他的选项类型，取反选项，以及可选参数的选项</a></li> <li><a href="#%e5%bf%85%e5%a1%ab%e9%80%89%e9%a1%b9">必填选项</a></li> <li><a href="#%e5%8f%98%e9%95%bf%e5%8f%82%e6%95%b0%e9%80%89%e9%a1%b9">变长参数选项</a></li> <li><a href="#%e7%89%88%e6%9c%ac%e9%80%89%e9%a1%b9">版本选项</a></li> <li><a href="#%E5%85%B6%E4%BB%96%E9%80%89%E9%A1%B9%E9%85%8D%E7%BD%AE">其他选项配置</a></li> <li><a href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E9%80%89%E9%A1%B9%E5%A4%84%E7%90%86">自定义选项处理</a></li></ul></li> <li><a href="#%e5%91%bd%e4%bb%a4">命令</a> <ul><li><a href="#%E5%91%BD%E4%BB%A4%E5%8F%82%E6%95%B0">命令参数</a> <ul><li><a href="#%E5%85%B6%E4%BB%96%E5%8F%82%E6%95%B0%E9%85%8D%E7%BD%AE">其他参数配置</a></li> <li><a href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%82%E6%95%B0%E5%A4%84%E7%90%86">自定义参数处理</a></li></ul></li> <li><a href="#%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0">处理函数</a></li> <li><a href="#%E7%8B%AC%E7%AB%8B%E7%9A%84%E5%8F%AF%E6%89%A7%E8%A1%8C%EF%BC%88%E5%AD%90%EF%BC%89%E5%91%BD%E4%BB%A4">独立的可执行（子）命令</a></li> <li><a href="#%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E9%92%A9%E5%AD%90">生命周期钩子</a></li></ul></li> <li><a href="#%e8%87%aa%e5%8a%a8%e5%8c%96%e5%b8%ae%e5%8a%a9%e4%bf%a1%e6%81%af">自动化帮助信息</a> <ul><li><a href="#%e8%87%aa%e5%ae%9a%e4%b9%89%e5%b8%ae%e5%8a%a9">自定义帮助</a></li> <li><a href="#%E5%9C%A8%E5%87%BA%E9%94%99%E5%90%8E%E5%B1%95%E7%A4%BA%E5%B8%AE%E5%8A%A9%E4%BF%A1%E6%81%AF">在出错后展示帮助信息</a></li> <li><a href="#%E4%BD%BF%E7%94%A8%E4%BB%A3%E7%A0%81%E5%B1%95%E7%A4%BA%E5%B8%AE%E5%8A%A9%E4%BF%A1%E6%81%AF">使用代码展示帮助信息</a></li> <li><a href="#name">.name</a></li> <li><a href="#usage">.usage</a></li> <li><a href="#description-%E5%92%8C-summary">.description 和 .summary</a></li> <li><a href="#helpoptionflags-description">.helpOption(flags, description)</a></li> <li><a href="#addhelpcommand">.addHelpCommand()</a></li> <li><a href="#%E5%85%B6%E4%BB%96%E5%B8%AE%E5%8A%A9%E9%85%8D%E7%BD%AE">其他帮助配置</a></li></ul></li> <li><a href="#%e8%87%aa%e5%ae%9a%e4%b9%89%e4%ba%8b%e4%bb%b6%e7%9b%91%e5%90%ac">自定义事件监听</a></li> <li><a href="#%e9%9b%b6%e7%a2%8e%e7%9f%a5%e8%af%86">零碎知识</a> <ul><li><a href="#parse-%e5%92%8c-parseasync">.parse() 和 .parseAsync()</a></li> <li><a href="#%E8%A7%A3%E6%9E%90%E9%85%8D%E7%BD%AE">解析配置</a></li> <li><a href="#%E4%BD%9C%E4%B8%BA%E5%B1%9E%E6%80%A7%E7%9A%84%E9%81%97%E7%95%99%E9%80%89%E9%A1%B9">作为属性的遗留选项</a></li> <li><a href="#typescript">TypeScript</a></li> <li><a href="#createCommand">createCommand()</a></li> <li><a href="#node-%E9%80%89%E9%A1%B9%EF%BC%8C%E5%A6%82---harmony">Node 选项，如 --harmony</a></li> <li><a href="#%e8%b0%83%e8%af%95%e5%ad%90%e5%91%bd%e4%bb%a4">调试子命令</a></li> <li><a href="#%E6%98%BE%E7%A4%BA%E9%94%99%E8%AF%AF">显示错误</a></li> <li><a href="#%E9%87%8D%E5%86%99%E9%80%80%E5%87%BA%E5%92%8C%E8%BE%93%E5%87%BA">重写退出和输出</a></li> <li><a href="#%E5%85%B6%E4%BB%96%E6%96%87%E6%A1%A3">其他文档</a></li></ul></li> <li><a href="#%e6%94%af%e6%8c%81">支持</a> <ul><li><a href="#%e4%bc%81%e4%b8%9a%e4%bd%bf%e7%94%a8-commander">企业使用 Commander</a></li></ul></li></ul></li></ul> <p>关于本文档中使用的术语，请见<a href="/10.大前端/1030.Node/docs/zh-CN/术语表.html">术语表</a></p> <h2 id="安装"><a href="#安装" class="header-anchor">#</a> 安装</h2> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">npm</span> <span class="token function">install</span> commander
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="快速开始"><a href="#快速开始" class="header-anchor">#</a> 快速开始</h2> <p>编写代码来描述你的命令行界面。
Commander 负责将参数解析为选项和命令参数，为问题显示使用错误，并实现一个有帮助的系统。</p> <p>Commander 是严格的，并且会针对无法识别的选项显示错误。
两种最常用的选项类型是布尔选项，和从参数中获取值的选项。</p> <p>示例代码：<a href="./examples/split.js">split.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token punctuation">{</span> program <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'commander'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'--first'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-s, --separator &lt;char&gt;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> options <span class="token operator">=</span> program<span class="token punctuation">.</span><span class="token function">opts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> limit <span class="token operator">=</span> options<span class="token punctuation">.</span>first <span class="token operator">?</span> <span class="token number">1</span> <span class="token operator">:</span> <span class="token keyword">undefined</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>program<span class="token punctuation">.</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span>options<span class="token punctuation">.</span>separator<span class="token punctuation">,</span> limit<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ node split.js -s / --fits a/b/c
error: unknown option '--fits'
(Did you mean --first?)
$ node split.js -s / --first a/b/c
[ 'a' ]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>这是一个使用子命令并带有帮助描述的更完整的程序。在多命令程序中，每个命令（或命令的独立可执行文件）都有一个操作处理程序。</p> <p>示例代码：<a href="./examples/string-util.js">string-util.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token punctuation">{</span> Command <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'commander'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> program <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Command</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program
  <span class="token punctuation">.</span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string">'string-util'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">description</span><span class="token punctuation">(</span><span class="token string">'CLI to some JavaScript string utilities'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">version</span><span class="token punctuation">(</span><span class="token string">'0.8.0'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program<span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'split'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">description</span><span class="token punctuation">(</span><span class="token string">'Split a string into substrings and display as an array'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">argument</span><span class="token punctuation">(</span><span class="token string">'&lt;string&gt;'</span><span class="token punctuation">,</span> <span class="token string">'string to split'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'--first'</span><span class="token punctuation">,</span> <span class="token string">'display just the first substring'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-s, --separator &lt;char&gt;'</span><span class="token punctuation">,</span> <span class="token string">'separator character'</span><span class="token punctuation">,</span> <span class="token string">','</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">action</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">str<span class="token punctuation">,</span> options</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> limit <span class="token operator">=</span> options<span class="token punctuation">.</span>first <span class="token operator">?</span> <span class="token number">1</span> <span class="token operator">:</span> <span class="token keyword">undefined</span><span class="token punctuation">;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span>options<span class="token punctuation">.</span>separator<span class="token punctuation">,</span> limit<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ node string-util.js help split
Usage: string-util split [options] &lt;string&gt;

Split a string into substrings and display as an array.

Arguments:
  string                  string to split

Options:
  --first                 display just the first substring
  -s, --separator &lt;char&gt;  separator character (default: &quot;,&quot;)
  -h, --help              display help for command

$ node string-util.js split --separator=/ a/b/c
[ 'a', 'b', 'c' ]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>更多示例可以在 <a href="https://github.com/tj/commander.js/tree/master/examples" target="_blank" rel="noopener noreferrer">examples<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 目录中找到。</p> <h2 id="声明-program-变量"><a href="#声明-program-变量" class="header-anchor">#</a> 声明 program 变量</h2> <p>为简化使用，Commander 提供了一个全局对象。本文档的示例代码均按此方法使用：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// CommonJS (.cjs)</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span> program <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'commander'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>如果程序较为复杂，用户需要以多种方式来使用 Commander，如单元测试等。创建本地 Command 对象是一种更好的方式：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// CommonJS (.cjs)</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span> Command <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'commander'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> program <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Command</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// ECMAScript (.mjs)</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Command <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'commander'</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> program <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Command</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-ts line-numbers-mode"><pre class="language-ts"><code><span class="token comment">// TypeScript (.ts)</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Command <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'commander'</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> program <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Command</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="选项"><a href="#选项" class="header-anchor">#</a> 选项</h2> <p>Commander 使用<code>.option()</code>方法来定义选项，同时可以附加选项的简介。每个选项可以定义一个短选项名称（-后面接单个字符）和一个长选项名称（--后面接一个或多个单词），使用逗号、空格或<code>|</code>分隔。</p> <p>解析后的选项可以通过<code>Command</code>对象上的<code>.opts()</code>方法获取，同时会被传递给命令处理函数。</p> <p>对于多个单词的长选项，选项名会转为驼峰命名法（camel-case），例如<code>--template-engine</code>选项可通过<code>program.opts().templateEngine</code>获取。</p> <p>选项及其选项参数可以用空格分隔，也可以组合成同一个参数。选项参数可以直接跟在短选项之后，也可以在长选项后面加上 <code>=</code>。</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>serve <span class="token parameter variable">-p</span> <span class="token number">80</span>
serve <span class="token parameter variable">-p80</span>
serve <span class="token parameter variable">--port</span> <span class="token number">80</span>
serve <span class="token parameter variable">--port</span><span class="token operator">=</span><span class="token number">80</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>--</code>可以标记选项的结束，后续的参数均不会被命令解释，可以正常使用。</p> <p>默认情况下，选项在命令行中的顺序不固定，一个选项可以在其他参数之前或之后指定。</p> <p>当<code>.opts()</code>不够用时，还有其他相关方法：</p> <ul><li><code>.optsWithGlobals()</code>返回合并的本地和全局选项值</li> <li><code>.getOptionValue()</code>和<code>.setOptionValue()</code>操作单个选项的值</li> <li><code>.getOptionValueSource()</code>和<code>.setOptionValueWithSource()</code>包括选项值的来源</li></ul> <h3 id="常用选项类型-boolean-型选项和带参数选项"><a href="#常用选项类型-boolean-型选项和带参数选项" class="header-anchor">#</a> 常用选项类型，boolean 型选项和带参数选项</h3> <p>有两种最常用的选项，一类是 boolean 型选项，选项无需配置参数，另一类选项则可以设置参数（使用尖括号声明在该选项后，如<code>--expect &lt;value&gt;</code>）。如果在命令行中不指定具体的选项及参数，则会被定义为<code>undefined</code>。</p> <p>示例代码：<a href="./examples/options-common.js">options-common.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-d, --debug'</span><span class="token punctuation">,</span> <span class="token string">'output extra debugging'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-s, --small'</span><span class="token punctuation">,</span> <span class="token string">'small pizza size'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-p, --pizza-type &lt;type&gt;'</span><span class="token punctuation">,</span> <span class="token string">'flavour of pizza'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>process<span class="token punctuation">.</span>argv<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> options <span class="token operator">=</span> program<span class="token punctuation">.</span><span class="token function">opts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>debug<span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>options<span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'pizza details:'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>small<span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'- small pizza size'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>pizzaType<span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">- </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>options<span class="token punctuation">.</span>pizzaType<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ pizza-options -p
error: option '-p, --pizza-type &lt;type&gt;' argument missing
$ pizza-options -d -s -p vegetarian
{ debug: true, small: true, pizzaType: 'vegetarian' }
pizza details:
- small pizza size
- vegetarian
$ pizza-options --pizza-type=cheese
pizza details:
- cheese
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>多个布尔短选项可以在破折号之后组合在一起，并且可以跟一个取值的单一选项。
例如 <code>-d -s -p cheese</code> 可以写成 <code>-ds -p cheese</code> 甚至 <code>-dsp cheese</code>。</p> <p>具有预期选项参数的选项是贪婪的，并且无论值如何，都会消耗参数。
所以 <code>--id -xyz</code> 读取 <code>-xyz</code> 作为选项参数。</p> <p>通过<code>program.parse(arguments)</code>方法处理参数，没有被使用的选项会存放在<code>program.args</code>数组中。该方法的参数是可选的，默认值为<code>process.argv</code>。</p> <h3 id="选项的默认值"><a href="#选项的默认值" class="header-anchor">#</a> 选项的默认值</h3> <p>选项可以设置一个默认值。</p> <p>示例代码：<a href="./examples/options-defaults.js">options-defaults.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-c, --cheese &lt;type&gt;'</span><span class="token punctuation">,</span> <span class="token string">'add the specified type of cheese'</span><span class="token punctuation">,</span> <span class="token string">'blue'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">cheese: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>program<span class="token punctuation">.</span><span class="token function">opts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>cheese<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ pizza-options
cheese: blue
$ pizza-options --cheese stilton
cheese: stilton
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="其他的选项类型-取反选项-以及可选参数的选项"><a href="#其他的选项类型-取反选项-以及可选参数的选项" class="header-anchor">#</a> 其他的选项类型，取反选项，以及可选参数的选项</h3> <p>可以定义一个以<code>no-</code>开头的 boolean 型长选项。在命令行中使用该选项时，会将对应选项的值置为<code>false</code>。当只定义了带<code>no-</code>的选项，未定义对应不带<code>no-</code>的选项时，该选项的默认值会被置为<code>true</code>。</p> <p>如果已经定义了<code>--foo</code>，那么再定义<code>--no-foo</code>并不会改变它本来的默认值。</p> <p>示例代码：<a href="./examples/options-negatable.js">options-negatable.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'--no-sauce'</span><span class="token punctuation">,</span> <span class="token string">'Remove sauce'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'--cheese &lt;flavour&gt;'</span><span class="token punctuation">,</span> <span class="token string">'cheese flavour'</span><span class="token punctuation">,</span> <span class="token string">'mozzarella'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'--no-cheese'</span><span class="token punctuation">,</span> <span class="token string">'plain with no cheese'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> options <span class="token operator">=</span> program<span class="token punctuation">.</span><span class="token function">opts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> sauceStr <span class="token operator">=</span> options<span class="token punctuation">.</span>sauce <span class="token operator">?</span> <span class="token string">'sauce'</span> <span class="token operator">:</span> <span class="token string">'no sauce'</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> cheeseStr <span class="token operator">=</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>cheese <span class="token operator">===</span> <span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">'no cheese'</span> <span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>options<span class="token punctuation">.</span>cheese<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> cheese</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">You ordered a pizza with </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>sauceStr<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> and </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>cheeseStr<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ pizza-options
You ordered a pizza with sauce and mozzarella cheese
$ pizza-options --sauce
error: unknown option '--sauce'
$ pizza-options --cheese=blue
You ordered a pizza with sauce and blue cheese
$ pizza-options --no-sauce --no-cheese
You ordered a pizza with no sauce and no cheese
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>选项的参数使用方括号声明表示参数是可选参数（如<code>--optional [value]</code>）。该选项在不带参数时可用作 boolean 选项，在带有参数时则从参数中得到值。</p> <p>示例代码：<a href="./examples/options-boolean-or-value.js">options-boolean-or-value.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-c, --cheese [type]'</span><span class="token punctuation">,</span> <span class="token string">'Add cheese with optional type'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>process<span class="token punctuation">.</span>argv<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> options <span class="token operator">=</span> program<span class="token punctuation">.</span><span class="token function">opts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>cheese <span class="token operator">===</span> <span class="token keyword">undefined</span><span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'no cheese'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>cheese <span class="token operator">===</span> <span class="token boolean">true</span><span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'add cheese'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">add cheese type </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>options<span class="token punctuation">.</span>cheese<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ pizza-options
no cheese
$ pizza-options --cheese
add cheese
$ pizza-options --cheese mozzarella
add cheese type mozzarella
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>带有可选选项参数的选项不是贪婪的，并且会忽略以破折号开头的参数。因此对于<code>--id -5</code>，<code>id</code>表现为布尔选项，但如果需要，您可以使用组合形式，例如 <code>--id=-5</code>。</p> <p>关于可能有歧义的用例，请见<a href="/10.大前端/1030.Node/docs/zh-CN/可变参数的选项.html">可变参数的选项</a>。</p> <h3 id="必填选项"><a href="#必填选项" class="header-anchor">#</a> 必填选项</h3> <p>通过<code>.requiredOption()</code>方法可以设置选项为必填。必填选项要么设有默认值，要么必须在命令行中输入，对应的属性字段在解析时必定会有赋值。该方法其余参数与<code>.option()</code>一致。</p> <p>示例代码：<a href="./examples/options-required.js">options-required.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">requiredOption</span><span class="token punctuation">(</span><span class="token string">'-c, --cheese &lt;type&gt;'</span><span class="token punctuation">,</span> <span class="token string">'pizza must have cheese'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ pizza
error: required option '-c, --cheese &lt;type&gt;' not specified
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="变长参数选项"><a href="#变长参数选项" class="header-anchor">#</a> 变长参数选项</h3> <p>定义选项时，可以通过使用<code>...</code>来设置参数为可变长参数。在命令行中，用户可以输入多个参数，解析后会以数组形式存储在对应属性字段中。在输入下一个选项前（<code>-</code>或<code>--</code>开头），用户输入的指令均会被视作变长参数。与普通参数一样的是，可以通过<code>--</code>标记当前命令的结束。</p> <p>示例代码：<a href="./examples/options-variadic.js">options-variadic.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-n, --number &lt;numbers...&gt;'</span><span class="token punctuation">,</span> <span class="token string">'specify numbers'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-l, --letter [letters...]'</span><span class="token punctuation">,</span> <span class="token string">'specify letters'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Options: '</span><span class="token punctuation">,</span> program<span class="token punctuation">.</span><span class="token function">opts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Remaining arguments: '</span><span class="token punctuation">,</span> program<span class="token punctuation">.</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ collect -n 1 2 3 --letter a b c
Options:  { number: [ '1', '2', '3' ], letter: [ 'a', 'b', 'c' ] }
Remaining arguments:  []
$ collect --letter=A -n80 operand
Options:  { number: [ '80' ], letter: [ 'A' ] }
Remaining arguments:  [ 'operand' ]
$ collect --letter -n 1 -n 2 3 -- operand
Options:  { number: [ '1', '2', '3' ], letter: true }
Remaining arguments:  [ 'operand' ]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>关于可能有歧义的用例，请见<a href="/10.大前端/1030.Node/docs/zh-CN/可变参数的选项.html">可变参数的选项</a>。</p> <h3 id="版本选项"><a href="#版本选项" class="header-anchor">#</a> 版本选项</h3> <p><code>.version()</code>方法可以设置版本，其默认选项为<code>-V</code>和<code>--version</code>，设置了版本后，命令行会输出当前的版本号。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program<span class="token punctuation">.</span><span class="token function">version</span><span class="token punctuation">(</span><span class="token string">'0.0.1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ ./examples/pizza -V
0.0.1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>版本选项也支持自定义设置选项名称，可以在<code>.version()</code>方法里再传递一些参数（长选项名称、描述信息），用法与<code>.option()</code>方法类似。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>program.version<span class="token punctuation">(</span><span class="token string">'0.0.1'</span>, <span class="token string">'-v, --vers'</span>, <span class="token string">'output the current version'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="其他选项配置"><a href="#其他选项配置" class="header-anchor">#</a> 其他选项配置</h3> <p>大多数情况下，选项均可通过<code>.option()</code>方法添加。但对某些不常见的用例，也可以直接构造<code>Option</code>对象，对选项进行更详尽的配置。</p> <p>示例代码：<a href="./examples/options-extra.js">options-extra.js</a>, <a href="./examples/options-env.js">options-env.js</a>, <a href="./examples/options-conflicts.js">options-conflicts.js</a>, <a href="./examples/options-implies.js">options-implies.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">addOption</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Option</span><span class="token punctuation">(</span><span class="token string">'-s, --secret'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">hideHelp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">addOption</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Option</span><span class="token punctuation">(</span><span class="token string">'-t, --timeout &lt;delay&gt;'</span><span class="token punctuation">,</span> <span class="token string">'timeout in seconds'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token number">60</span><span class="token punctuation">,</span> <span class="token string">'one minute'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">addOption</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Option</span><span class="token punctuation">(</span><span class="token string">'-d, --drink &lt;size&gt;'</span><span class="token punctuation">,</span> <span class="token string">'drink size'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">choices</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'small'</span><span class="token punctuation">,</span> <span class="token string">'medium'</span><span class="token punctuation">,</span> <span class="token string">'large'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">addOption</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Option</span><span class="token punctuation">(</span><span class="token string">'-p, --port &lt;number&gt;'</span><span class="token punctuation">,</span> <span class="token string">'port number'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">env</span><span class="token punctuation">(</span><span class="token string">'PORT'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">addOption</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Option</span><span class="token punctuation">(</span><span class="token string">'--donate [amount]'</span><span class="token punctuation">,</span> <span class="token string">'optional donation in dollars'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">preset</span><span class="token punctuation">(</span><span class="token string">'20'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">argParser</span><span class="token punctuation">(</span>parseFloat<span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">addOption</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Option</span><span class="token punctuation">(</span><span class="token string">'--disable-server'</span><span class="token punctuation">,</span> <span class="token string">'disables the server'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">conflicts</span><span class="token punctuation">(</span><span class="token string">'port'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">addOption</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Option</span><span class="token punctuation">(</span><span class="token string">'--free-drink'</span><span class="token punctuation">,</span> <span class="token string">'small drink included free '</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">implies</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">drink</span><span class="token operator">:</span> <span class="token string">'small'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ extra --help
Usage: help [options]

Options:
  -t, --timeout &lt;delay&gt;  timeout in seconds (default: one minute)
  -d, --drink &lt;size&gt;     drink cup size (choices: &quot;small&quot;, &quot;medium&quot;, &quot;large&quot;)
  -p, --port &lt;number&gt;    port number (env: PORT)
  --donate [amount]      optional donation in dollars (preset: &quot;20&quot;)
  --disable-server       disables the server
  --free-drink           small drink included free
  -h, --help             display help for command

$ extra --drink huge
error: option '-d, --drink &lt;size&gt;' argument 'huge' is invalid. Allowed choices are small, medium, large.

$ PORT=80 extra --donate --free-drink
Options:  { timeout: 60, donate: 20, port: '80', freeDrink: true, drink: 'small' }

$ extra --disable-server --port 8000
error: option '--disable-server' cannot be used with option '-p, --port &lt;number&gt;'
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="自定义选项处理"><a href="#自定义选项处理" class="header-anchor">#</a> 自定义选项处理</h3> <p>选项的参数可以通过自定义函数来处理，该函数接收两个参数，即用户新输入的参数值和当前已有的参数值（即上一次调用自定义处理函数后的返回值），返回新的选项参数值。</p> <p>自定义函数适用场景包括参数类型转换，参数暂存，或者其他自定义处理的场景。</p> <p>可以在自定义函数的后面设置选项参数的默认值或初始值（例如参数用列表暂存时需要设置一个初始空列表)。</p> <p>示例代码：<a href="./examples/options-custom-processing.js">options-custom-processing.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">myParseInt</span><span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> dummyPrevious</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// parseInt 参数为字符串和进制数</span>
  <span class="token keyword">const</span> parsedValue <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>value<span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isNaN</span><span class="token punctuation">(</span>parsedValue<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">commander<span class="token punctuation">.</span>InvalidArgumentError</span><span class="token punctuation">(</span><span class="token string">'Not a number.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> parsedValue<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">increaseVerbosity</span><span class="token punctuation">(</span><span class="token parameter">dummyValue<span class="token punctuation">,</span> previous</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> previous <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">collect</span><span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> previous</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> previous<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">[</span>value<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">commaSeparatedList</span><span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> dummyPrevious</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> value<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

program
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-f, --float &lt;number&gt;'</span><span class="token punctuation">,</span> <span class="token string">'float argument'</span><span class="token punctuation">,</span> parseFloat<span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-i, --integer &lt;number&gt;'</span><span class="token punctuation">,</span> <span class="token string">'integer argument'</span><span class="token punctuation">,</span> myParseInt<span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-v, --verbose'</span><span class="token punctuation">,</span> <span class="token string">'verbosity that can be increased'</span><span class="token punctuation">,</span> increaseVerbosity<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-c, --collect &lt;value&gt;'</span><span class="token punctuation">,</span> <span class="token string">'repeatable value'</span><span class="token punctuation">,</span> collect<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-l, --list &lt;items&gt;'</span><span class="token punctuation">,</span> <span class="token string">'comma separated list'</span><span class="token punctuation">,</span> commaSeparatedList<span class="token punctuation">)</span>
<span class="token punctuation">;</span>

program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> options <span class="token operator">=</span> program<span class="token punctuation">.</span><span class="token function">opts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>float <span class="token operator">!==</span> <span class="token keyword">undefined</span><span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">float: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>options<span class="token punctuation">.</span>float<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>integer <span class="token operator">!==</span> <span class="token keyword">undefined</span><span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">integer: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>options<span class="token punctuation">.</span>integer<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>verbose <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">verbosity: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>options<span class="token punctuation">.</span>verbose<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>collect<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>options<span class="token punctuation">.</span>collect<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>list <span class="token operator">!==</span> <span class="token keyword">undefined</span><span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>options<span class="token punctuation">.</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ custom -f 1e2
float: 100
$ custom --integer 2
integer: 2
$ custom -v -v -v
verbose: 3
$ custom -c a -c b -c c
[ 'a', 'b', 'c' ]
$ custom --list x,y,z
[ 'x', 'y', 'z' ]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h2 id="命令"><a href="#命令" class="header-anchor">#</a> 命令</h2> <p>通过<code>.command()</code>或<code>.addCommand()</code>可以配置命令，有两种实现方式：为命令绑定处理函数，或者将命令单独写成一个可执行文件（详述见后文）。子命令支持嵌套（<a href="./examples/nestedCommands.js">示例代码</a>）。</p> <p><code>.command()</code>的第一个参数为命令名称。命令参数可以跟在名称后面，也可以用<code>.argument()</code>单独指定。参数可为必选的（尖括号表示）、可选的（方括号表示）或变长参数（点号表示，如果使用，只能是最后一个参数）。</p> <p>使用<code>.addCommand()</code>向<code>program</code>增加配置好的子命令。</p> <p>例如:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 通过绑定处理函数实现命令（这里的指令描述为放在`.command`中）</span>
<span class="token comment">// 返回新生成的命令（即该子命令）以供继续配置</span>
program
  <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'clone &lt;source&gt; [destination]'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">description</span><span class="token punctuation">(</span><span class="token string">'clone a repository into a newly created directory'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">action</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">source<span class="token punctuation">,</span> destination</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'clone command called'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 通过独立的的可执行文件实现命令 (注意这里指令描述是作为`.command`的第二个参数)</span>
<span class="token comment">// 返回最顶层的命令以供继续添加子命令</span>
program
  <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'start &lt;service&gt;'</span><span class="token punctuation">,</span> <span class="token string">'start named service'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'stop [service]'</span><span class="token punctuation">,</span> <span class="token string">'stop named service, or all if no name supplied'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 分别装配命令</span>
<span class="token comment">// 返回最顶层的命令以供继续添加子命令</span>
program
  <span class="token punctuation">.</span><span class="token function">addCommand</span><span class="token punctuation">(</span>build<span class="token punctuation">.</span><span class="token function">makeBuildCommand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>使用<code>.command()</code>和<code>addCommand()</code>来指定选项的相关设置。当设置<code>hidden: true</code>时，该命令不会打印在帮助信息里。当设置<code>isDefault: true</code>时，若没有指定其他子命令，则会默认执行这个命令（<a href="./examples/defaultCommand.js">样例</a>）。</p> <h3 id="命令参数"><a href="#命令参数" class="header-anchor">#</a> 命令参数</h3> <p>如上所述，子命令的参数可以通过<code>.command()</code>指定。对于有独立可执行文件的子命令来说，参数只能以这种方法指定。而对其他子命令，参数也可用以下方法。</p> <p>在<code>Command</code>对象上使用<code>.argument()</code>来按次序指定命令参数。该方法接受参数名称和参数描述。参数可为必选的（尖括号表示，例如<code>&lt;required&gt;</code>）或可选的（方括号表示，例如<code>[optional]</code>）。</p> <p>示例代码：<a href="./examples/argument.js">argument.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">version</span><span class="token punctuation">(</span><span class="token string">'0.1.0'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">argument</span><span class="token punctuation">(</span><span class="token string">'&lt;username&gt;'</span><span class="token punctuation">,</span> <span class="token string">'user to login'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">argument</span><span class="token punctuation">(</span><span class="token string">'[password]'</span><span class="token punctuation">,</span> <span class="token string">'password for user, if required'</span><span class="token punctuation">,</span> <span class="token string">'no password given'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">action</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">username<span class="token punctuation">,</span> password</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'username:'</span><span class="token punctuation">,</span> username<span class="token punctuation">)</span><span class="token punctuation">;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'password:'</span><span class="token punctuation">,</span> password<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>在参数名后加上<code>...</code>来声明可变参数，且只有最后一个参数支持这种用法。可变参数会以数组的形式传递给处理函数。例如：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">version</span><span class="token punctuation">(</span><span class="token string">'0.1.0'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'rmdir'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">argument</span><span class="token punctuation">(</span><span class="token string">'&lt;dirs...&gt;'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">action</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">dirs</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    dirs<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">dir</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'rmdir %s'</span><span class="token punctuation">,</span> dir<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>有一种便捷方式可以一次性指定多个参数，但不包含参数描述：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">arguments</span><span class="token punctuation">(</span><span class="token string">'&lt;username&gt; &lt;password&gt;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="其他参数配置"><a href="#其他参数配置" class="header-anchor">#</a> 其他参数配置</h4> <p>有少数附加功能可以直接构造<code>Argument</code>对象，对参数进行更详尽的配置。</p> <p>示例代码：<a href="./examples/arguments-extra.js">arguments-extra.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">addArgument</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">commander<span class="token punctuation">.</span>Argument</span><span class="token punctuation">(</span><span class="token string">'&lt;drink-size&gt;'</span><span class="token punctuation">,</span> <span class="token string">'drink cup size'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">choices</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'small'</span><span class="token punctuation">,</span> <span class="token string">'medium'</span><span class="token punctuation">,</span> <span class="token string">'large'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">addArgument</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">commander<span class="token punctuation">.</span>Argument</span><span class="token punctuation">(</span><span class="token string">'[timeout]'</span><span class="token punctuation">,</span> <span class="token string">'timeout in seconds'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token number">60</span><span class="token punctuation">,</span> <span class="token string">'one minute'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="自定义参数处理"><a href="#自定义参数处理" class="header-anchor">#</a> 自定义参数处理</h4> <p>选项的参数可以通过自定义函数来处理（与处理选项参数时类似），该函数接收两个参数：用户新输入的参数值和当前已有的参数值（即上一次调用自定义处理函数后的返回值），返回新的命令参数值。</p> <p>处理后的参数值会传递给命令处理函数，同时可通过<code>.processedArgs</code>获取。</p> <p>可以在自定义函数的后面设置命令参数的默认值或初始值。</p> <p>示例代码：<a href="./examples/arguments-custom-processing.js">arguments-custom-processing.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'add'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">argument</span><span class="token punctuation">(</span><span class="token string">'&lt;first&gt;'</span><span class="token punctuation">,</span> <span class="token string">'integer argument'</span><span class="token punctuation">,</span> myParseInt<span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">argument</span><span class="token punctuation">(</span><span class="token string">'[second]'</span><span class="token punctuation">,</span> <span class="token string">'integer argument'</span><span class="token punctuation">,</span> myParseInt<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">action</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">first<span class="token punctuation">,</span> second</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>first<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> + </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>second<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> = </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>first <span class="token operator">+</span> second<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="处理函数"><a href="#处理函数" class="header-anchor">#</a> 处理函数</h3> <p>命令处理函数的参数，为该命令声明的所有参数，除此之外还会附加两个额外参数：一个是解析出的选项，另一个则是该命令对象自身。</p> <p>示例代码：<a href="./examples/thank.js">thank.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">argument</span><span class="token punctuation">(</span><span class="token string">'&lt;name&gt;'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-t, --title &lt;honorific&gt;'</span><span class="token punctuation">,</span> <span class="token string">'title to use before name'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-d, --debug'</span><span class="token punctuation">,</span> <span class="token string">'display some debugging'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">action</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> options<span class="token punctuation">,</span> command</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>debug<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">'Called %s with options %o'</span><span class="token punctuation">,</span> command<span class="token punctuation">.</span><span class="token function">name</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> options<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">const</span> title <span class="token operator">=</span> options<span class="token punctuation">.</span>title <span class="token operator">?</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>options<span class="token punctuation">.</span>title<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> </span><span class="token template-punctuation string">`</span></span> <span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">Thank-you </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>title<span class="token interpolation-punctuation punctuation">}</span></span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>name<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>如果你愿意，你可以跳过为处理函数声明参数直接使用 command。 <code>this</code> 关键字设置为运行命令，可以在函数表达式中使用（但不能从箭头函数中使用）。</p> <p>示例代码：<a href="./examples/action-this.js">action-this.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'serve'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">argument</span><span class="token punctuation">(</span><span class="token string">'&lt;script&gt;'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-p, --port &lt;number&gt;'</span><span class="token punctuation">,</span> <span class="token string">'port number'</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">action</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">'Run script %s on port %s'</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">opts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>port<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>处理函数支持<code>async</code>，相应的，需要使用<code>.parseAsync</code>代替<code>.parse</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">/* 在这里编写代码 */</span> <span class="token punctuation">}</span>

<span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  program
    <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'run'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">action</span><span class="token punctuation">(</span>run<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">await</span> program<span class="token punctuation">.</span><span class="token function">parseAsync</span><span class="token punctuation">(</span>process<span class="token punctuation">.</span>argv<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>使用命令时，所给的选项和命令参数会被验证是否有效。凡是有未知的选项，或缺少所需的命令参数，都会报错。
如要允许使用未知的选项，可以调用<code>.allowUnknownOption()</code>。默认情况下，传入过多的参数并不报错，但也可以通过调用<code>.allowExcessArguments(false)</code>来启用过多参数的报错。</p> <h3 id="独立的可执行-子-命令"><a href="#独立的可执行-子-命令" class="header-anchor">#</a> 独立的可执行（子）命令</h3> <p>当<code>.command()</code>带有描述参数时，就意味着使用独立的可执行文件作为子命令。
Commander 会尝试在入口脚本的目录中搜索名称组合为 <code>command-subcommand</code> 的文件，如以下示例中的 <code>pm-install</code> 或 <code>pm-search</code>。搜索包括尝试常见的文件扩展名，如<code>.js</code>。
你可以使用 <code>executableFile</code> 配置选项指定自定义名称（和路径）。
你可以使用 <code>.executableDir()</code> 为子命令指定自定义搜索目录。</p> <p>你可以在可执行文件里处理（子）命令的选项，而不必在顶层声明它们。</p> <p>示例代码：<a href="./examples/pm">pm</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string">'pm'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">version</span><span class="token punctuation">(</span><span class="token string">'0.1.0'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'install [name]'</span><span class="token punctuation">,</span> <span class="token string">'install one or more packages'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'search [query]'</span><span class="token punctuation">,</span> <span class="token string">'search with optional query'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'update'</span><span class="token punctuation">,</span> <span class="token string">'update installed packages'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">executableFile</span><span class="token operator">:</span> <span class="token string">'myUpdateSubCommand'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">'list'</span><span class="token punctuation">,</span> <span class="token string">'list packages installed'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">isDefault</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>process<span class="token punctuation">.</span>argv<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>如果该命令需要支持全局安装，请确保有对应的权限，例如<code>755</code>。</p> <h3 id="生命周期钩子"><a href="#生命周期钩子" class="header-anchor">#</a> 生命周期钩子</h3> <p>可以在命令的生命周期事件上设置回调函数。</p> <p>示例代码：<a href="./examples/hook.js">hook.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-t, --trace'</span><span class="token punctuation">,</span> <span class="token string">'display trace statements for commands'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">hook</span><span class="token punctuation">(</span><span class="token string">'preAction'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">thisCommand<span class="token punctuation">,</span> actionCommand</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>thisCommand<span class="token punctuation">.</span><span class="token function">opts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>trace<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">About to call action handler for subcommand: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>actionCommand<span class="token punctuation">.</span><span class="token function">name</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'arguments: %O'</span><span class="token punctuation">,</span> actionCommand<span class="token punctuation">.</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'options: %o'</span><span class="token punctuation">,</span> actionCommand<span class="token punctuation">.</span><span class="token function">opts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>钩子函数支持<code>async</code>，相应的，需要使用<code>.parseAsync</code>代替<code>.parse</code>。一个事件上可以添加多个钩子。</p> <p>支持的事件有：</p> <table><thead><tr><th style="text-align:left;">事件名称</th> <th style="text-align:left;">触发时机</th> <th style="text-align:left;">参数列表</th></tr></thead> <tbody><tr><td style="text-align:left;"><code>preAction</code>, <code>postAction</code></td> <td style="text-align:left;">本命令或其子命令的处理函数执行前/后</td> <td style="text-align:left;"><code>(thisCommand, actionCommand)</code></td></tr> <tr><td style="text-align:left;"><code>preSubcommand</code></td> <td style="text-align:left;">在其直接子命令解析之前调用</td> <td style="text-align:left;"><code>(thisCommand, subcommand)</code></td></tr></tbody></table> <h2 id="自动化帮助信息"><a href="#自动化帮助信息" class="header-anchor">#</a> 自动化帮助信息</h2> <p>帮助信息是 Commander 基于你的程序自动生成的，默认的帮助选项是<code>-h,--help</code>。</p> <p>示例代码：<a href="./examples/pizza">pizza</a></p> <div class="language-console line-numbers-mode"><pre class="language-text"><code>$ node ./examples/pizza --help
Usage: pizza [options]

An application for pizza ordering

Options:
  -p, --peppers        Add peppers
  -c, --cheese &lt;type&gt;  Add the specified type of cheese (default: &quot;marble&quot;)
  -C, --no-cheese      You do not want any cheese
  -h, --help           display help for command
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>如果你的命令中包含了子命令，会默认添加<code>help</code>命令，它可以单独使用，也可以与子命令一起使用来提示更多帮助信息。用法与<code>shell</code>程序类似：</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>shell <span class="token builtin class-name">help</span>
shell <span class="token parameter variable">--help</span>

shell <span class="token builtin class-name">help</span> spawn
shell spawn <span class="token parameter variable">--help</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="自定义帮助"><a href="#自定义帮助" class="header-anchor">#</a> 自定义帮助</h3> <p>可以添加额外的帮助信息，与内建的帮助一同展示。</p> <p>示例代码：<a href="./examples/custom-help">custom-help</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-f, --foo'</span><span class="token punctuation">,</span> <span class="token string">'enable some foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

program<span class="token punctuation">.</span><span class="token function">addHelpText</span><span class="token punctuation">(</span><span class="token string">'after'</span><span class="token punctuation">,</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">

Example call:
  $ custom-help --help</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>将会输出以下的帮助信息：</p> <div class="language-Text line-numbers-mode"><pre class="language-text"><code>Usage: custom-help [options]

Options:
  -f, --foo   enable some foo
  -h, --help  display help for command

Example call:
  $ custom-help --help
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>位置参数对应的展示方式如下：</p> <ul><li><code>beforeAll</code>：作为全局标头栏展示</li> <li><code>before</code>：在内建帮助信息之前展示</li> <li><code>after</code>：在内建帮助信息之后展示</li> <li><code>afterAll</code>：作为全局末尾栏展示</li></ul> <p><code>beforeAll</code>和<code>afterAll</code>两个参数作用于命令及其所有的子命令。</p> <p>第二个参数可以是一个字符串，也可以是一个返回字符串的函数。对后者而言，为便于使用，该函数可以接受一个上下文对象，它有如下属性：</p> <ul><li><code>error</code>：boolean 值，代表该帮助信息是否由于不当使用而展示</li> <li><code>command</code>：代表展示该帮助信息的<code>Command</code>对象</li></ul> <h3 id="在出错后展示帮助信息"><a href="#在出错后展示帮助信息" class="header-anchor">#</a> 在出错后展示帮助信息</h3> <p>默认情况下，出现命令用法错误时只会显示错误信息。可以选择在出错后展示完整的帮助或自定义的帮助信息。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program<span class="token punctuation">.</span><span class="token function">showHelpAfterError</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 或者</span>
program<span class="token punctuation">.</span><span class="token function">showHelpAfterError</span><span class="token punctuation">(</span><span class="token string">'(add --help for additional information)'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ pizza --unknown
error: unknown option '--unknown'
(add --help for additional information)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>默认行为是在出现未知命令或选项错误后建议正确拼写。你可以禁用此功能。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program<span class="token punctuation">.</span><span class="token function">showSuggestionAfterError</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-console line-numbers-mode"><pre class="language-text"><code>$ pizza --hepl
error: unknown option '--hepl'
(Did you mean --help?)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="使用代码展示帮助信息"><a href="#使用代码展示帮助信息" class="header-anchor">#</a> 使用代码展示帮助信息</h3> <p><code>.help()</code>：展示帮助信息并退出。可以通过传入<code>{ error: true }</code>来让帮助信息从 stderr 输出，并以代表错误的状态码退出程序。</p> <p><code>.outputHelp()</code>：只展示帮助信息，不退出程序。传入<code>{ error: true }</code>可以让帮助信息从 stderr 输出。</p> <p><code>.helpInformation()</code>：得到字符串形式的内建的帮助信息，以便用于自定义的处理及展示。</p> <h3 id="name"><a href="#name" class="header-anchor">#</a> .name</h3> <p>命令名称出现在帮助中，也用于定位独立的可执行子命令。</p> <p>你可以使用 <code>.name()</code> 或在 Command 构造函数中指定程序名称。对于 program ，Commander 会使用传递给 <code>.parse()</code> 的完整参数中的脚本名称。但是，脚本名称会根据程序的启动方式而有所不同，因此您可能希望明确指定它。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program<span class="token punctuation">.</span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string">'pizza'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> pm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Command</span><span class="token punctuation">(</span><span class="token string">'pm'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>使用 <code>.command()</code> 指定时，子命令会获得名称。如果您自己创建子命令以与 <code>.addCommand()</code> 一起使用，则使用 <code>.name()</code> 或在 Command 构造函数中设置名称。</p> <h3 id="usage"><a href="#usage" class="header-anchor">#</a> .usage</h3> <p>通过这个选项可以修改帮助信息的首行提示，例如：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string">&quot;my-command&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">usage</span><span class="token punctuation">(</span><span class="token string">&quot;[global options] command&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>帮助信息开头如下：</p> <div class="language-Text line-numbers-mode"><pre class="language-text"><code>Usage: my-command [global options] command
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="description-和-summary"><a href="#description-和-summary" class="header-anchor">#</a> .description 和 .summary</h3> <p>description 出现在命令的帮助中。当列为程序的子命令时，你可以选择提供更短的 summary 以供使用。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string">&quot;duplicate&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">summary</span><span class="token punctuation">(</span><span class="token string">&quot;make a copy&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">description</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">Make a copy of the current project.
This may require additional disk space.
  </span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="helpoption-flags-description"><a href="#helpoption-flags-description" class="header-anchor">#</a> .helpOption(flags, description)</h3> <p>每一个命令都带有一个默认的帮助选项。你可以改变 <code>flags</code> 和 <code>description</code> 参数。传入 <code>false</code> 则会禁用内建的帮助信息。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">helpOption</span><span class="token punctuation">(</span><span class="token string">'-e, --HELP'</span><span class="token punctuation">,</span> <span class="token string">'read more information'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="addhelpcommand"><a href="#addhelpcommand" class="header-anchor">#</a> .addHelpCommand()</h3> <p>如果一个命令拥有子命令，它也将有一个默认的帮助子命令。使用<code>.addHelpCommand()</code>和<code>.addHelpCommand(false)</code>可以打开或关闭默认的帮助子命令。</p> <p>也可以自定义名字和描述：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program<span class="token punctuation">.</span><span class="token function">addHelpCommand</span><span class="token punctuation">(</span><span class="token string">'assist [command]'</span><span class="token punctuation">,</span> <span class="token string">'show assistance'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="其他帮助配置"><a href="#其他帮助配置" class="header-anchor">#</a> 其他帮助配置</h3> <p>内建帮助信息通过<code>Help</code>类进行格式化。如有需要，可以使用<code>.configureHelp()</code>来更改其数据属性和方法，或使用<code>.createHelp()</code>来创建子类，从而配置<code>Help</code>类的行为。</p> <p>数据属性包括：</p> <ul><li><code>helpWidth</code>：指明帮助信息的宽度。可在单元测试中使用。</li> <li><code>sortSubcommands</code>：以字母序排列子命令</li> <li><code>sortOptions</code>：以字母序排列选项</li></ul> <p>可以得到可视化的参数列表，选项列表，以及子命令列表。列表的每个元素都具有<code>_term_</code>和<code>_description_</code>属性，并可以对其进行格式化。关于其使用方式，请参考<code>.formatHelp()</code>。</p> <p>示例代码：<a href="./examples/configure-help.js">configure-help.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program<span class="token punctuation">.</span><span class="token function">configureHelp</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token literal-property property">sortSubcommands</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
  <span class="token function-variable function">subcommandTerm</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">cmd</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> cmd<span class="token punctuation">.</span><span class="token function">name</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 显示名称，而非用法</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="自定义事件监听"><a href="#自定义事件监听" class="header-anchor">#</a> 自定义事件监听</h2> <p>监听命令和选项可以执行自定义函数。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'option:verbose'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  process<span class="token punctuation">.</span>env<span class="token punctuation">.</span><span class="token constant">VERBOSE</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">opts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>verbose<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="零碎知识"><a href="#零碎知识" class="header-anchor">#</a> 零碎知识</h2> <h3 id="parse-和-parseasync"><a href="#parse-和-parseasync" class="header-anchor">#</a> .parse() 和 .parseAsync()</h3> <p><code>.parse</code>的第一个参数是要解析的字符串数组，也可以省略参数而使用<code>process.argv</code>。</p> <p>如果参数遵循与 node 不同的约定，可以在第二个参数中传递<code>from</code>选项：</p> <ul><li><code>node</code>：默认值，<code>argv[0]</code>是应用，<code>argv[1]</code>是要跑的脚本，后续为用户参数；</li> <li><code>electron</code>：<code>argv[1]</code>根据 electron 应用是否打包而变化；</li> <li><code>user</code>：来自用户的所有参数。</li></ul> <p>例如：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>process<span class="token punctuation">.</span>argv<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 指明，按 node 约定</span>
program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 默认，自动识别 electron</span>
program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'-f'</span><span class="token punctuation">,</span> <span class="token string">'filename'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">from</span><span class="token operator">:</span> <span class="token string">'user'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="解析配置"><a href="#解析配置" class="header-anchor">#</a> 解析配置</h3> <p>当默认的解析方式无法满足需要，Commander 也提供了其他的解析行为。</p> <p>默认情况下，程序的选项在子命令前后均可被识别。如要只允许选项出现在子命令之前，可以使用<code>.enablePositionalOptions()</code>。这样可以在命令和子命令中使用意义不同的同名选项。</p> <p>示例代码：<a href="./examples/positional-options.js">positional-options.js</a></p> <p>当启用了带顺序的选项解析，以下程序中，<code>-b</code>选项在第一行中将被解析为程序顶层的选项，而在第二行中则被解析为子命令的选项：</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>program <span class="token parameter variable">-b</span> subcommand
program subcommand <span class="token parameter variable">-b</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>默认情况下，选项在命令参数前后均可被识别。如要使选项仅在命令参数前被识别，可以使用<code>.passThroughOptions()</code>。这样可以把参数和跟随的选项传递给另一程序，而无需使用<code>--</code>来终止选项解析。
如要在子命令中使用此功能，必须首先启用带顺序的选项解析。</p> <p>示例代码：<a href="./examples/pass-through-options.js">pass-through-options.js</a></p> <p>当启用此功能时，以下程序中，<code>--port=80</code>在第一行中会被解析为程序的选项，而在第二行中则会被解析为一个命令参数：</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>program <span class="token parameter variable">--port</span><span class="token operator">=</span><span class="token number">80</span> arg
program arg <span class="token parameter variable">--port</span><span class="token operator">=</span><span class="token number">80</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>默认情况下，使用未知选项会提示错误。如要将未知选项视作普通命令参数，并继续处理其他部分，可以使用<code>.allowUnknownOption()</code>。这样可以混用已知和未知的选项。</p> <p>默认情况下，传入过多的命令参数并不会报错。可以使用<code>.allowExcessArguments(false)</code>来启用这一检查。</p> <h3 id="作为属性的遗留选项"><a href="#作为属性的遗留选项" class="header-anchor">#</a> 作为属性的遗留选项</h3> <p>在 Commander 7 以前，选项的值是作为属性存储在命令对象上的。
这种处理方式便于实现，但缺点在于，选项可能会与<code>Command</code>的已有属性相冲突。通过使用<code>.storeOptionsAsProperties()</code>，可以恢复到这种旧的处理方式，并可以不加改动地继续运行遗留代码。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program
  <span class="token punctuation">.</span><span class="token function">storeOptionsAsProperties</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token string">'-d, --debug'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">action</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">commandAndOptions</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>commandAndOptions<span class="token punctuation">.</span>debug<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">Called </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>commandAndOptions<span class="token punctuation">.</span><span class="token function">name</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="typescript"><a href="#typescript" class="header-anchor">#</a> TypeScript</h3> <p>如果你使用 ts-node，并有<code>.ts</code>文件作为独立可执行文件，那么需要用 node 运行你的程序以使子命令能正确调用，例如：</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">node</span> <span class="token parameter variable">-r</span> ts-node/register pm.ts
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="createcommand"><a href="#createcommand" class="header-anchor">#</a> createCommand()</h3> <p>使用这个工厂方法可以创建新命令，此时不需要使用<code>new</code>，如</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>const <span class="token punctuation">{</span> createCommand <span class="token punctuation">}</span> <span class="token operator">=</span> require<span class="token punctuation">(</span><span class="token string">'commander'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
const program <span class="token operator">=</span> createCommand<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>createCommand</code>同时也是<code>Command</code>对象的一个方法，可以创建一个新的命令（而非子命令），使用<code>.command()</code>创建子命令时内部会调用该方法，具体使用方式可参考 <a href="./examples/custom-command-class.js">custom-command-class.js</a>。</p> <h3 id="node-选项-如-harmony"><a href="#node-选项-如-harmony" class="header-anchor">#</a> Node 选项，如 --harmony</h3> <p>要使用<code>--harmony</code>等选项有以下两种方式：</p> <ul><li>在子命令脚本中加上<code>#!/usr/bin/env node --harmony</code>。注：Windows 系统不支持；</li> <li>调用时加上<code>--harmony</code>参数，例如<code>node --harmony examples/pm publish</code>。<code>--harmony</code>选项在开启子进程时仍会保留。</li></ul> <h3 id="调试子命令"><a href="#调试子命令" class="header-anchor">#</a> 调试子命令</h3> <p>一个可执行的子命令会作为单独的子进程执行。</p> <p>如果使用 node inspector 的<code>node -inspect</code>等命令来<a href="https://nodejs.org/en/docs/guides/debugging-getting-started/" target="_blank" rel="noopener noreferrer">调试<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>可执行命令，对于生成的子命令，inspector 端口会递增 1。</p> <p>如果想使用 VSCode 调试，则需要在<code>launch.json</code>配置文件里设置<code>&quot;autoAttachChildProcesses&quot;: true</code>。</p> <h3 id="显示错误"><a href="#显示错误" class="header-anchor">#</a> 显示错误</h3> <p>你可用于针对自己的错误情况调用 Commander 错误处理。（另请参阅下一节有关退出处理的内容）</p> <p>除了错误消息，你还可以选择指定 <code>exitCode</code>（与 <code>process.exit</code> 一起使用）和 <code>code</code>（与 <code>CommanderError</code> 一起使用）</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">'Password must be longer than four characters'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
program<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">'Custom processing has failed'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">exitCode</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token literal-property property">code</span><span class="token operator">:</span> <span class="token string">'my.custom.error'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="重写退出和输出"><a href="#重写退出和输出" class="header-anchor">#</a> 重写退出和输出</h3> <p>默认情况下，在检测到错误、打印帮助信息或版本信息时 Commander 会调用<code>process.exit</code>方法。其默认实现会抛出一个<code>CommanderError</code>，可以重写该方法并提供一个回调函数（可选）。</p> <p>回调函数的参数为<code>CommanderError</code>，属性包括 Number 型的<code>exitCode</code>、String 型的<code>code</code>和<code>message</code>。子命令完成调用后会开始异步处理。正常情况下，打印错误信息、帮助信息或版本信息不会被重写影响，因为重写会发生在打印之后。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>program<span class="token punctuation">.</span><span class="token function">exitOverride</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">try</span> <span class="token punctuation">{</span>
  program<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>process<span class="token punctuation">.</span>argv<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 自定义处理...</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>Commander 默认用作命令行应用，其输出写入 stdout 和 stderr。
对于其他应用类型，这一行为可以修改。并且可以修改错误信息的展示方式。</p> <p>示例代码：<a href="./examples/configure-output.js">configure-output.js</a></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">errorColor</span><span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 添加 ANSI 转义字符，以将文本输出为红色</span>
  <span class="token keyword">return</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">\x1b[31m</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>str<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">\x1b[0m</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

program
  <span class="token punctuation">.</span><span class="token function">configureOutput</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    <span class="token comment">// 此处使输出变得容易区分</span>
    <span class="token function-variable function">writeOut</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> process<span class="token punctuation">.</span>stdout<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">[OUT] </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>str<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token function-variable function">writeErr</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> process<span class="token punctuation">.</span>stdout<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">[ERR] </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>str<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token comment">// 将错误高亮显示</span>
    <span class="token function-variable function">outputError</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">str<span class="token punctuation">,</span> write</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token function">write</span><span class="token punctuation">(</span><span class="token function">errorColor</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="其他文档"><a href="#其他文档" class="header-anchor">#</a> 其他文档</h3> <p>请参考关于如下话题的其他文档：</p> <ul><li><a href="/10.大前端/1030.Node/docs/zh-CN/不再推荐使用的功能.html">不再推荐使用的功能</a>。这些功能仍受到支持，以保证向后兼容。</li> <li><a href="/10.大前端/1030.Node/docs/zh-CN/可变参数的选项.html">可变参数的选项</a></li></ul> <h2 id="支持"><a href="#支持" class="header-anchor">#</a> 支持</h2> <p>当前版本的 Commander 在 LTS 版本的 Node.js 上完全支持。并且至少需要 v14。
（使用更低版本 Node.js 的用户建议安装更低版本的 Commander）</p> <p>社区支持请访问项目的 <a href="https://github.com/tj/commander.js/issues" target="_blank" rel="noopener noreferrer">Issues<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。</p> <h3 id="企业使用-commander"><a href="#企业使用-commander" class="header-anchor">#</a> 企业使用 Commander</h3> <p>作为 Tidelift 订阅的一部分。</p> <p>Commander 和很多其他包的维护者已与 Tidelift 合作，面向企业提供开源依赖的商业支持与维护。企业可以向相关依赖包的维护者支付一定的费用，帮助企业节省时间，降低风险，改进代码运行情况。<a href="https://tidelift.com/subscription/pkg/npm-commander?utm_source=npm-commander&amp;utm_medium=referral&amp;utm_campaign=enterprise&amp;utm_term=repo" target="_blank" rel="noopener noreferrer">了解更多<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></div></div>  <div class="page-edit"><!----> <!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2023/06/10, 12:06:41</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/f3d9dd/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">node全局脚本原理解析</div></a> <a href="/pages/34a68b/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">制作自己的npm包</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/f3d9dd/" class="prev">node全局脚本原理解析</a></span> <span class="next"><a href="/pages/34a68b/">制作自己的npm包</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/e3ef00/"><div>
            接雨水
            <!----></div></a> <span class="date">04-08</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/a5eeb6/"><div>
            vim日常使用记录
            <!----></div></a> <span class="date">04-08</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/d46779/"><div>
            有效的字母异位词
            <!----></div></a> <span class="date">04-08</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:izhaong@outlook.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/izhaong" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2021-2024
    <span>izhaong | <a href="https://github.com/izhaong" target="_blank">github</a> | <a href="http://beian.miit.gov.cn/" target="_blank">蜀ICP备2021031194号</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <div class="body-bg" style="background:url() center center / cover no-repeat;opacity:0.5;"></div> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.6d26e8d4.js" defer></script><script src="/assets/js/2.8273876b.js" defer></script><script src="/assets/js/51.acd0d321.js" defer></script>
  </body>
</html>
